import 'package:flutter/services.dart';
import 'channel_error_handler.dart';

/// 第三方插件Channel - 负责第三方服务集成
/// 处理登录、支付、分享、推送等第三方服务
class PluginChannel {
  static const MethodChannel _channel = MethodChannel('plugin_channel');

  /// 谷歌登录
  static Future<Map<String, dynamic>> googleLogin() async {
    try {
      final result = await _channel.invokeMethod('googleLogin');
      final response = Map<String, dynamic>.from(result);
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '谷歌登录', 'PluginChannel');
      rethrow;
    }
  }

  /// 微信登录
  static Future<Map<String, dynamic>> wechatLogin() async {
    try {
      final result = await _channel.invokeMethod('wechatLogin');
      final response = Map<String, dynamic>.from(result);
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '微信登录', 'PluginChannel');
      rethrow;
    }
  }

  /// 苹果登录
  static Future<Map<String, dynamic>> appleLogin() async {
    try {
      final result = await _channel.invokeMethod('appleLogin');
      final response = Map<String, dynamic>.from(result);
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '苹果登录', 'PluginChannel');
      rethrow;
    }
  }

  /// 支付宝支付
  static Future<Map<String, dynamic>> alipayPayment({required String orderId, required double amount}) async {
    try {
      final paymentInfo = {'orderId': orderId, 'amount': amount, 'timestamp': DateTime.now().millisecondsSinceEpoch};
      final result = await _channel.invokeMethod('alipayPayment', paymentInfo);
      final response = Map<String, dynamic>.from(result);
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '支付宝支付', 'PluginChannel');
      rethrow;
    }
  }

  /// 微信支付
  static Future<Map<String, dynamic>> wechatPayment({required String orderId, required double amount}) async {
    try {
      final paymentInfo = {'orderId': orderId, 'amount': amount, 'timestamp': DateTime.now().millisecondsSinceEpoch};
      final result = await _channel.invokeMethod('wechatPayment', paymentInfo);
      final response = Map<String, dynamic>.from(result);
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '微信支付', 'PluginChannel');
      rethrow;
    }
  }

  /// 苹果支付
  static Future<Map<String, dynamic>> applePay({required String orderId, required double amount}) async {
    try {
      final paymentInfo = {'orderId': orderId, 'amount': amount, 'timestamp': DateTime.now().millisecondsSinceEpoch};
      final result = await _channel.invokeMethod('applePay', paymentInfo);
      final response = Map<String, dynamic>.from(result);
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '苹果支付', 'PluginChannel');
      rethrow;
    }
  }

  /// 分享内容
  static Future<bool> shareContent({required String title, required String content, String? url, String? imagePath}) async {
    try {
      final shareInfo = {'title': title, 'content': content, 'url': url, 'imagePath': imagePath};
      final result = await _channel.invokeMethod('shareContent', shareInfo);
      return result ?? false;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '分享内容', 'PluginChannel');
      rethrow;
    }
  }

  /// 分享到微信
  static Future<bool> shareToWechat({required String title, required String content, String? url, String? imagePath}) async {
    try {
      final shareInfo = {'title': title, 'content': content, 'url': url, 'imagePath': imagePath};
      final result = await _channel.invokeMethod('shareToWechat', shareInfo);
      return result ?? false;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '分享到微信', 'PluginChannel');
      rethrow;
    }
  }

  /// 注册推送通知
  static Future<Map<String, dynamic>> registerPushNotification() async {
    try {
      final result = await _channel.invokeMethod('registerPushNotification');
      final response = Map<String, dynamic>.from(result);
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '注册推送通知', 'PluginChannel');
      rethrow;
    }
  }

  /// 发送本地推送
  static Future<bool> sendLocalNotification({required String title, required String body, String? payload}) async {
    try {
      final notificationInfo = {'title': title, 'body': body, 'payload': payload};
      final result = await _channel.invokeMethod('sendLocalNotification', notificationInfo);
      return result ?? false;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '发送本地推送', 'PluginChannel');
      rethrow;
    }
  }
}
